import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.*;

/**
 * @Author: Aerospace
 * @Date: 2023/11/21 9:10
 * @Description: Dream begins
 */
public class Schedule {
    public static void main(String[] args) throws InterruptedException {
//        testThreadPool();
//        testCallable();
//        testCancel();
        testExceptionCancel();
    }





    public static void testCallable(){
        //1.创建线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3,5,6, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        //2.给任务线程池处理
        pool.submit(new Collabledemo(1000));
    }

    public static void testThreadPool(){
        //1.创建线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3,5,6, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        //2.给任务线程池处理
        MyRunnable target = new MyRunnable();
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
    }

    public void testFix(){
        ExecutorService pool = Executors.newFixedThreadPool(3);
        pool.execute(new MyRunnable());
        pool.execute(new MyRunnable());
        pool.execute(new MyRunnable());
        pool.execute(new MyRunnable());
    }

    public static void testCancel() throws InterruptedException {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        System.out.println("start : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("work : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        },5 ,5, TimeUnit.SECONDS);
        TimeUnit.SECONDS.sleep(15);
        scheduledFuture.cancel(true);
        System.out.println("cancel : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        TimeUnit.SECONDS.sleep(30);
    }

    public static void testExceptionCancel() throws InterruptedException {
        ScheduledFuture<?> scheduledFuture = null;
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        System.out.println("start : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        try{
            scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(() -> {
                System.out.println("work : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//                try{
//                    TimeUnit.SECONDS.sleep(1);
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
                throw new RuntimeException("test");
            },5 ,5, TimeUnit.SECONDS);

        }catch (Exception e){
            e.printStackTrace();
        }
        TimeUnit.SECONDS.sleep(15);
        scheduledFuture.cancel(true);
        System.out.println("cancel : " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        TimeUnit.SECONDS.sleep(30);
    }
}
